package com.asia5b.a5mall.middleware.model.member

import androidx.databinding.ObservableField
import com.asia5b.a5mall.middleware.model.base.BaseModel
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.google.gson.annotations.SerializedName
import java.io.Serializable

/**
 * Created by zhangyuncai on 2020/1/6.
 * todo 国家/州/城市/区,后台是错开的
 */
class AddressModel : BaseModel(), MultiItemEntity {

    /*国家ID*/
    @SerializedName(value = "countryId", alternate = ["province_id", "ship_county_id"])
    var countryId = 0

    /*国家名称*/
    @SerializedName(value = "countryName", alternate = ["province", "ship_county"])
    var countryName = ""


    /**省份ID*/
    @SerializedName(value = "provinceId", alternate = ["city_id", "ship_province_id"])
    var provinceId = 0

    /**省份名称*/
    @SerializedName(value = "provinceName", alternate = ["city", "ship_province"])
    var provinceName = ""


    /**城市ID*/
    @SerializedName(value = "cityId", alternate = ["county_id", "ship_city_id"])
    var cityId = 0

    /**城市名称*/
    @SerializedName(value = "cityName", alternate = ["county", "ship_city"])
    var cityName = ""


    /**城镇ID*/
    @SerializedName(value = "townId", alternate = ["town_id", "ship_town_id"])
    var townId = 0

    /**城镇名称*/
    @SerializedName(value = "townName", alternate = ["town", "ship_town"])
    var townName = ""


    /*视图类型*/
    var type = 0

    /**地址ID*/
    @SerializedName(value = "addr_id", alternate = ["address_id"])
    var addr_id = 0

    /*会员id*/
    var member_id = 0

    /**收货人姓名*/
    @SerializedName(value = "name", alternate = ["ship_name"])
    var name = ""


    /**详细地址*/
    @SerializedName(value = "addr", alternate = ["ship_addr"])
    var addr = ""


    //电话
    @SerializedName(value = "tel", alternate = ["ship_tel"])
    var tel = ""


    /**手机号*/
    var mobile = ""

    var mobilePrivacyText = ""
        get() {
            if (mobile.length > 5) {
                var privacyText = ""
                val toCharArray = mobile.toCharArray()
                for (i in toCharArray.indices) {
                    if ((i in 0..2) || (i in (toCharArray.size - 2)..(toCharArray.size - 1))) {
                        privacyText += toCharArray[i]
                    } else {
                        privacyText += "*"
                    }
                }
                return privacyText
            } else {
                return mobile
            }
        }

    /*默认地址==1*/
    private var def_addr = 0
    var isDefault = false
        set(value) {
            field = value
            if (value) {
                def_addr = 1
            } else {
                def_addr = 0
            }
        }
        get() {
            return def_addr == 1
        }

    /**标签*/
    var ship_address_name = ""

    var isShipAddressNameEmpty = false
        get() {
            return ship_address_name.isNotEmpty()
        }

    @SerializedName(value = "region_id", alternate = ["warehouse_id"])
    var region_id = 0

    //国家区号
    var gr = ""


    // 详细地址拼接国家省市区(镇)
    var detailAddress = ""
        get() {
            return "$countryName$provinceName$cityName$townName  $addr"
        }

    //收货人
    val nameOb = ObservableField<String>()

    //手机号
    val mobileOb = ObservableField<String>()

    //详细地址
    val detailAddressOb = ObservableField<String>()

    //国家省市区(镇)
    val regionTextOB = ObservableField<String>()

    //国家区号
    val grOb = ObservableField<String>()

    //标签
    val labelOb = ObservableField<String>()

    //是否默认
    val defaultAddressOb = ObservableField<Boolean>(false)
    override val itemType: Int
        get() = type


}

/*
{
 "addr_id": 230,
 "member_id": 186,
 "name": "武时亿3",
 "country": "中国",
 "province_id": 55388,
 "city_id": 55765,
 "county_id": 57271,
 "town_id": 57978,
 "county": "东城",
 "city": "北京市",
 "province": "中国（China）",
 "town": "uuuuu",
 "addr": "天河大道13号",
 "tel": "86",
 "mobile": "13000000003",
 "def_addr": 1,
 "ship_address_name": "武时亿3标签",
 "region_id": 95,
 "gr": null
},
 */